<?php
include 'JHeader.php';

$joomla_prefix = 'joomla_';
$response = '';
if(isset($_GET['q']))
{
    $name = $_GET['q'];
    /*
     * Predictions
     */
    require 'Database/Database.php';
    $db = new Database();
    $db->ChangeDatabase('joomla'); // Joomla-DB
    $db->Connect();
    $sql   = 'SELECT name, id FROM ' . $joomla_prefix . 'users WHERE name LIKE \'%' . $name . '%\' AND block = 0';
    $users = $db->ExecSQL($sql);
    $db->Disconnect();
    $db->ChangeDatabase('ranking');
    $db->Connect();
    $sql           = 'SELECT JID FROM Admins WHERE Name LIKE \'%' . $name . '%\'';
    $active_admins = $db->ExecSQL($sql);
    $db->Disconnect();

    if($users)
    {
        if(sizeof($users) <= 10)
        {
            $response = '<table>';
            foreach($users as $user)
            {
                $isAdmin = false;
                if($active_admins)
                {
                    foreach($active_admins as $active_admin)
                    {
                        if($active_admin->JID == $user->id)
                        {
                            $isAdmin = true;
                            break;
                        }
                    }
                }

                if($isAdmin)
                {
                    $response .= '<tr><td>' . $user->name . '</td><td>';
                    $response .= 'Is already an admin.</td></tr>';
                }
                else
                {
                    $response .= '<tr><td>' . htmlentities($user->name) . '</td><td>';
                    $response .= '<a href="add_admin.php?id=' . $user->id . '&name=' . htmlentities($user->name) . '"><img src="Icons/add_user.png" /></a></td></tr>';
                }
            }
            $response .= '</table>';
        }
        else
        {
            $response = 'Too many results.';
        }
    }
    else
    {
        $response = 'No results.';
    }
}
else
{
    $response = 'ERROR';
}

echo $response;
?>